<template>
  <div>
    <boxNeedForm ref="boxNum" :seeFlag="see" planTag="Van" @setTerm="setTerm"></boxNeedForm>
    <unpackingForm type="packing" ref="addPlan" :Tag="tag"  @add="add" :seeFlag="see" planTag="BOX"></unpackingForm>
    <newLeftMenu2 ref="newLeftMenu2" v-show="false"></newLeftMenu2>
  </div>
</template>

<script>
import boxNeedForm from './module/boxNeedForm.vue'
import unpackingForm from './module/unpackingForm.vue'
import newLeftMenu2 from '@/components/newLeftMenu2.vue'
export default {
  name: 'packingNoNum',
  components: { boxNeedForm, unpackingForm ,newLeftMenu2},
  data() {
    return {
      tag:'',
      see:false,
    }
  },
//通过路由规则，离开该组件时被调用
beforeRouteLeave (to, from, next) {
      console.log('About--beforeRouteLeave',to,from)
      let nowJsonData = JSON.stringify(this.$refs.boxNum.form)
      let oldFormJson = JSON.stringify(this.$refs.boxNum.oriForm)
      // let nowJsonData1 = JSON.stringify(this.$refs.addPlan.form)
      // let oldFormJson1 = JSON.stringify(this.$refs.addPlan.oriForm)
      // console.log('================================ ',nowJsonData,' 000000000000000000000000 ',oldFormJson) //;
      if(nowJsonData!=oldFormJson && to.query.name != 'boxGoods' && !this.see){
      // if(nowJsonData!=oldFormJson || nowJsonData1!=oldFormJson1){
      // if(this.tableData!= []){
        this.$nextTick(()=>{
          this.$confirm('存在未提交信息，确认关闭？',{
            closeOnHashChange:false,
            closeOnClickModal:false,
            distinguishCancelAndClose:true,
            closeOnPressEscape:false,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
          })
                .then(_ => {
                  console.log('这边有问题',this.$route.path);
                  // console.log('这边有问题',this.$route.current);
                  this.$store.commit('setActiveMenuPath',next.fullPath)
                  // this.$router.push(to.fullPath)
                  next()
                })
                .catch(_ => {
                  console.log('是取消！！！！！！！！！！！！！！！！！',from.fullPath);
                  this.$store.commit('setActiveMenuPath',from.fullPath)
                  // this.$route.path = from.fullPath
                  // this.$router.push(from.fullPath)
                  // console.log('看看路由',this.$route.path)
                  this.$nextTick(()=>{
                    // this.$refs.newLeftMenu2.refresh = !this.$refs.newLeftMenu2.refresh
                    let path = {
                      index: from.fullPath,
                    }
                    // this.$refs.newLeftMenu2.getActivePath(path) 
                    this.$refs.newLeftMenu2.getPath(from.fullPath)
                    this.$refs.newLeftMenu2.linkTab(from.fullPath)
                  })
                  // this.$refs.newLeftMenu2.linkTab(from.fullPath)
                  // console.log('path: ' + from.fullPath,this.$route.path);
                });
        })
      }else{
        next()
      }
  },
  methods: {
    setTerm(val){
      console.log(val)
      this.$refs.addPlan.form.termCode=val
      this.$refs.addPlan.$forceUpdate()
    },
    getInfo(){
      this.$nextTick(()=>{
        if(undefined!==this.$route.query.id){
          if('see'===this.$route.query.state){
            this.see=true
          }else{
            this.see=false
          }
          this.$api.contentSelectAll({
            id:this.$route.query.id
          }).then(res=>{
            let data=res.data.data[0]
            let name=this.$refs.addPlan.form.cstCoperName
            let plan=this.$refs.addPlan.form.planTagName1
            this.$refs.addPlan.form=JSON.parse(JSON.stringify(data))
            this.$refs.addPlan.form.cstCoperName=name
            this.$refs.addPlan.form.planTagName1=plan
            
            this.$refs.addPlan.$refs.myUpload.file=JSON.parse(JSON.stringify(data.planAttachmentList))
            //console.log(this.$refs.addPlan.form)
            this.$refs.boxNum.form=JSON.parse(JSON.stringify(data.vanUseRequirements))
            this.$refs.boxNum.form.termCode=JSON.parse(JSON.stringify(data)).termCode
          }).catch()
        }
      })
      
    },
    set(val){
      console.log(val)
      this.$refs.addPlan.set(val)
    },
    link(){
      this.$router.push({
        query:{
          name:'boxGoods'
        },
        name:'success'
      })
    },
    checkBox(){
      let result=false
      this.$refs.boxNum.$refs.form.validate((valid) => {
            result=valid
      });
      return result
    },
    checkPlan(){
      let result=false
      this.$refs.addPlan.$refs.form.validate((valid) => {
            result=valid
      });
      return result
    },
    add(){
      this.$refs.addPlan.loading=true
      let that=this
      let formBox = this.checkBox()
      let formPlan = this.checkPlan()
      if(!(formBox&&formPlan)){
        this.$refs.addPlan.loading=false
        return 
      }
      //console.log
      let boxNum=this.$refs.boxNum.form
      let form=this.$refs.addPlan.form
      form.vanUseRequirements=boxNum
      form.termCode=this.$refs.boxNum.form.termCode
      form.vanUseRequirements.internalfg=form.tradeFlag
      form.ivoyId=form.voyId
      if('Y'===form.tradeFlag){
        form.vanUseRequirements.stsCstatuscd='BE'
      }else{
        form.vanUseRequirements.stsCstatuscd='IE'
      }
      
      let par=''
      if(undefined===form.id||null===form.id){
        par='ZX_Add'
      }else{
        par='ZX_Edit'
      }
      this.$api.shipE(form,par).then(res=>{
        if(200===res.code){
          this.$message.success({message:undefined!==form.id?'编辑成功!':'新增成功!',onClose(){that.link()}})
        }else{
          this.$refs.addPlan.loading=false
          //this.$message.error(res.message)
        }
      }).catch(res=>{
        this.$refs.addPlan.loading=false
        //this.$message.error(res.message)
      })
    }
  },

  computed: {},

  mounted() {
    this.tag='BOX2'
    this.getInfo()
  },
}
</script>
<style lang='less' scoped>
/deep/ .el-input.is-disabled .el-input__inner {
  color: #666

}
/deep/ .el-textarea.is-disabled .el-textarea__inner {
  color: #666
}
</style>